home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 1.iso
/
toolbox
/
src
/
exampleCode
/
opengl
/
XLayerInfo
/
layerdemo.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-11-11
|
7KB
|
216 lines
/*
* Copyright (c) 1993-94, Silicon Graphics, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that the name of Silicon Graphics may not be used in any advertising or
* publicity relating to the software without the specific, prior written
* permission of Silicon Graphics.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
* INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE
* POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* OpenGL(TM) is a trademark of Silicon Graphics, Inc.
*/
/* $Revision: 1.2 $ */
/* compile: cc -o layerdemo layerdemo.c XLayerUtil.c -lX11 -lm */
#include <stdio.h>
#include <math.h>
#include <sys/types.h>
#include <unistd.h>
#include <GL/gl.h>
#include <GL/glx.h>
#include "XLayerUtil.h"
#define SIZE 400 /* width and height of window */
Display *dpy;
Window root, win, overlay;
Colormap cmap;
int screen, white, nVisuals, status;
GC overlayGC;
XEvent event;
XLayerVisualInfo template;
XLayerVisualInfo *otherLayerInfo, *defaultLayerInfo;
XSetWindowAttributes swa;
XGCValues gcvals;
XColor color, exact;
int x = 0, y = SIZE/2;
int first_time = 1;
GLXContext cx;
XVisualInfo * normal_vi = NULL;
static void
initGL (void)
{
cx = glXCreateContext(dpy, normal_vi, 0, True);
glXMakeCurrent(dpy, win, cx);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glClearDepth(1.0);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluPerspective(40.0, 1.0, 10.0, 200.0);
glTranslatef(0.0, 0.0, -50.0);
glRotatef(-58.0, 0.0, 1.0, 0.0);
}
redrawNormalPlanes()
{
printf("%d : redrawNormalPlanes\n", getpid());
if (first_time) {
initGL ();
first_time = 0;
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON);
glColor3f(0.0, 0.0, 0.0);
glVertex3f(-10.0, -10.0, 0.0);
glColor3f(0.7, 0.7, 0.7);
glVertex3f(10.0, -10.0, 0.0);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(-10.0, 10.0, 0.0);
glEnd();
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 0.0);
glVertex3f(0.0, -10.0, -10.0);
glColor3f(0.0, 1.0, 0.7);
glVertex3f(0.0, -10.0, 10.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 5.0, -10.0);
glEnd();
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 0.0);
glVertex3f(-10.0, 6.0, 4.0);
glColor3f(1.0, 0.0, 1.0);
glVertex3f(-10.0, 3.0, 4.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(4.0, -9.0, -10.0);
glColor3f(1.0, 0.0, 1.0);
glVertex3f(4.0, -6.0, -10.0);
glEnd();
glXSwapBuffers(dpy, win);
glFlush();
}
#define MESSAGE1 "This text is in the"
#define MESSAGE2 "OVERLAY PLANES"
redrawOverlayPlanes()
{
printf("%d : redrawOverlayPlanes\n", getpid());
XDrawString(dpy, overlay, overlayGC, x, y, MESSAGE1, sizeof(MESSAGE1)-1);
XDrawString(dpy, overlay, overlayGC, x, y + 15, MESSAGE2, sizeof(MESSAGE2)-1);
}
fatalError(message)
char *message;
{
fprintf(stderr, "layerdemo: %s\n", message);
exit(1);
}
static int attribs[] = {GLX_RGBA, GLX_DOUBLEBUFFER,
GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1,
GLX_DEPTH_SIZE, 1,
None};
main(argc, argv)
int argc;
char *argv[];
{
dpy = XOpenDisplay(NULL);
if(dpy == NULL) fatalError("cannot open display");
screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen);
normal_vi = glXChooseVisual(dpy, screen, attribs);
cmap = XCreateColormap(dpy, root, normal_vi->visual, AllocNone);
swa.colormap = cmap;
swa.event_mask = ExposureMask;
swa.border_pixel = 0;
win = XCreateWindow(dpy, root, 10, 10, SIZE, SIZE, 0, normal_vi->depth,
InputOutput, normal_vi->visual,
CWColormap | CWEventMask | CWBorderPixel, &swa);
XStoreName(dpy, win, "Overlay Test");
XSetWMColormapWindows(dpy, win, &win, 1);
/* find layer of default visual */
template.vinfo.visualid = normal_vi->visualid;
defaultLayerInfo = XGetLayerVisualInfo(dpy, VisualIDMask, &template, &nVisuals);
/* look for visual in layer "above" default visual with transparent pixel */
template.layer = defaultLayerInfo->layer + 1;
template.vinfo.screen = screen;
template.type = TransparentPixel;
otherLayerInfo = XGetLayerVisualInfo(dpy,
VisualScreenMask|VisualLayerMask|VisualTransparentType,
&template, &nVisuals);
/* otherLayerInfo++; */
printf("Overlay visual id is %X\n", otherLayerInfo->vinfo.visualid);
/* XCreateColormap uses AllocNone for 2 reasons:
* 1) haven't determined class of visual, visual could have static colormap
* and more importantly
* 2) transparent pixel might make AllocAll impossible.
*/
cmap = XCreateColormap(dpy, root, otherLayerInfo->vinfo.visual, AllocNone);
/* not default colormap, must find our own black and white */
status = XAllocNamedColor(dpy, cmap, "white", &color, &exact);
if(status == 0) {
fatalError("could not allocate white");
}
white = color.pixel;
swa.border_pixel = white;
swa.background_pixel = otherLayerInfo->value;
swa.colormap = cmap;
swa.event_mask = ExposureMask | ButtonPressMask;
overlay = XCreateWindow(dpy, win, 0, 0, SIZE, SIZE, 0,
otherLayerInfo->vinfo.depth,
InputOutput, otherLayerInfo->vinfo.visual,
CWColormap | CWEventMask | CWBorderPixel | CWBackPixel,
&swa);
XSetWMColormapWindows(dpy, win, &overlay, 1);
gcvals.foreground = white;
gcvals.function = GXxor;
overlayGC = XCreateGC(dpy, overlay, GCForeground | GCFunction, &gcvals);
XMapSubwindows(dpy, win);
XMapWindow(dpy, win);
while(1) {
XNextEvent(dpy, &event);
switch(event.type) {
case Expose:
if(event.xexpose.window == win)
redrawNormalPlanes();
else
redrawOverlayPlanes();
break;
case ButtonPress:
x = random() % SIZE/2;
y = random() % SIZE;
XClearWindow(dpy, overlay);
redrawOverlayPlanes();
break;
}
}
}